//使用
// scale.switch_sl(num,system);/*num是是十进制值，system是想要转换进制的位数*/
function random_char(n) {
	//获取n位随机值
	var random_charset = [
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
		'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
	];
	var res = "";
	for (var i = 0; i < n; i++) {
		var id = Math.ceil(Math.random() * 35);
		res += random_charset[id];
	}
	return res;
}

function switch_sl(num, system) {
	// 十进制转其它进制，仅支持到十六进制
	var shiliu = [
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G'
		],
		sl_arr = [];
	do {
		var fl_sl = Math.floor(num / system);

		var yu_sl = num % system;
		sl_arr.unshift(shiliu[yu_sl]);
		num = fl_sl;
	} while (num > 0)
	return sl_arr.join('');
}
// 字符串转16进制
function strToHexCharCode(str) {
	if (str === "")
		return "";
	var hexCharCode = [];
	// hexCharCode.push("0x");
	for (var i = 0; i < str.length; i++) {
		hexCharCode.push((str.charCodeAt(i)).toString(16));
	}
	return hexCharCode.join("");
}
// 16进制转字符串
function hexCharCodeToStr(hexCharCodeStr) {
	var trimedStr = hexCharCodeStr.trim();
	var rawStr =
		trimedStr.substr(0, 2).toLowerCase() === "0x" ?
		trimedStr.substr(2) :
		trimedStr;
	var len = rawStr.length;
	if (len % 2 !== 0) {
		alert("Illegal Format ASCII Code!");
		return "";
	}
	var curCharCode;
	var resultStr = [];
	for (var i = 0; i < len; i = i + 2) {
		curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
		resultStr.push(String.fromCharCode(curCharCode));
	}
	return resultStr.join("");
}

//十六进制字符串转字节数组
function Str2Bytes(str) {
	var pos = 0;
	var len = str.length;
	if (len % 2 != 0) {
		return null;
	}

	len /= 2;
	var hexA = new Array();
	for (var i = 0; i < len; i++) {
		var s = str.substr(pos, 2);
		var v = parseInt(s, 16);
		hexA.push(v);
		pos += 2;
	}
	return hexA;
}
//字节数组转十六进制字符串
function Bytes2Str(arr) {
	var str = "";
	for (var i = 0; i < arr.length; i++) {
		var tmp = arr[i].toString(16);
		if (tmp.length == 1) {
			tmp = "0" + tmp;
		}
		str += tmp;
	}
	return str;
}
//byte数组转字符串
function byteToString(arr) {
	if (typeof arr === 'string') {
		return arr;
	}
	var str = '',
		_arr = arr;
	for (var i = 0; i < _arr.length; i++) {
		var one = _arr[i].toString(2),
			v = one.match(/^1+?(?=0)/);
		if (v && one.length == 8) {
			var bytesLength = v[0].length;
			var store = _arr[i].toString(2).slice(7 - bytesLength);
			for (var st = 1; st < bytesLength; st++) {
				store += _arr[st + i].toString(2).slice(2);
			}
			str += String.fromCharCode(parseInt(store, 2));
			i += bytesLength - 1;
		} else {
			str += String.fromCharCode(_arr[i]);
		}
	}
	return str;
}
// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
  const hexArr = Array.prototype.map.call(
	new Uint8Array(buffer),
	function (bit) {
	  return ('00' + bit.toString(16)).slice(-2)
	}
  )
  return hexArr.join('')
}

export default {
	switch_sl,
	strToHexCharCode,
	hexCharCodeToStr,
	Str2Bytes,
	Bytes2Str,
	byteToString,
	ab2hex
}
